﻿using System;
using System.Collections.Generic;
using System.Text;

using gudusoft.gsqlparser;
using gudusoft.gsqlparser.Units;

/*
 * ref: http://www.dpriver.com/blog/list-of-demos-illustrate-how-to-use-general-sql-parser/parse-the-values-and-field-names-in-where-clause/
 */


namespace columnInWhereClause
{
    class columnInWhereClause
    {
        static void Main(string[] args)
        {
            TGSqlParser sqlparser = new TGSqlParser(TDbVendor.DbVMssql);
            sqlparser.SqlText.Text = @"Select firstname, lastname, age from Clients where State = ""CA"" and  City = ""Hollywood""";
            int i = sqlparser.Parse();
            if (i == 0)
            {
                whereCondition w = new whereCondition(sqlparser.SqlStatements[0].WhereClause);
                w.printColumn();

            }
            else
                Console.WriteLine(sqlparser.ErrorMessages);

        }
    }

    class whereCondition
    {

        private TLzCustomExpression condition;
        public whereCondition(TLzCustomExpression expr)
        {
            this.condition = expr;
        }

        public void printColumn()
        {
            this.condition.InOrderTraverse(searchColumnVisitor);
        }

        

        public Boolean searchColumnVisitor(TLz_Node pnode, Boolean pIsLeafNode)
        {
            TLzCustomExpression lcexpr = (TLzCustomExpression)pnode;
            if (lcexpr.lexpr is TLzCustomExpression)
            {
                TLzCustomExpression leftExpr = (TLzCustomExpression)lcexpr.lexpr;
                if (leftExpr.oper == TLzOpType.Expr_Attr)
                {
                    Console.WriteLine("column: {0}",leftExpr.AsText);
                    if (lcexpr.opname != null)
                    {
                        Console.WriteLine("Operator: {0}",lcexpr.opname.AsText);
                    }
                    Console.WriteLine("value: {0}", lcexpr.rexpr.AsText);
                    Console.WriteLine("");
                }
            }

            return true;
        }

    }

}
